from rest_framework.decorators import permission_classes
from rest_framework import viewsets, permissions
from apps.Basic_data.models import Units, Suppliers, Clients, Product, GoodsCategory
from apps.Basic_data.serializers import BasicUnitSerializer, BasicSupplierSerializer, \
    BasicGoodsSerializer, BasicClientSerializer, BasicGoodsCategorySerializer
from rest_framework.filters import SearchFilter
from rest_framework.schemas import AutoSchema
from utils.pagination import MyPagination


@permission_classes([permissions.IsAuthenticated])
class BaseViewSet(viewsets.ModelViewSet):
    """
    基础视图集，用于减少重复代码。
    """
    permission_classes = [permissions.IsAuthenticated]  # 设置权限为需要认证的用户
    pagination_class = MyPagination  # 设置分页类
    # 可以添加一些通用的设置，例如权限控制、分页等


class GoodsCategoryAPIView(BaseViewSet):
    queryset = GoodsCategory.objects.all()
    serializer_class = BasicGoodsCategorySerializer


# 定义UnitAPIView类，处理单位信息的增删改查
class UnitAPIView(BaseViewSet):
    queryset = Units.objects.all()
    serializer_class = BasicUnitSerializer


# # 定义ColorAPIView类，处理颜色信息的增删改查
# class ColorAPIView(BaseViewSet):
#     queryset = Colors.objects.all()
#     serializer_class = BasicColorSerializer


# 定义SuppliersAPIView类，处理供应商信息的增删改查
class SuppliersAPIView(BaseViewSet):
    queryset = Suppliers.objects.all()
    serializer_class = BasicSupplierSerializer


# 定义GoodsAPIView类，处理商品信息的增删改查
class GoodsAPIView(BaseViewSet):
    queryset = Product.objects.all()  # 查询集，指定要操作的模型
    serializer_class = BasicGoodsSerializer  # 序列化器，指定要使用的序列化器
    filter_backends = [SearchFilter]  # 添加搜索过滤器
    search_fields = ['name', 'code']  # 指定搜索字段


# 定义ClientAPIView类，处理客户信息的增删改查
class ClientAPIView(BaseViewSet):
    queryset = Clients.objects.all()
    serializer_class = BasicClientSerializer
